package me.zhengjie.modules.location.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.location.domain.Location;
import me.zhengjie.modules.location.service.LocationService;
import me.zhengjie.modules.location.service.dto.LocationDto;
import me.zhengjie.modules.location.service.dto.LocationQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.util.List;
import java.util.Set;

/**
* @author TianI
* @date 2022-01-28
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "项目区位管理")
@RequestMapping("/api/location")
public class LocationController {

    private final LocationService locationService;

    @GetMapping
    @Log("查询项目区位")
    @ApiOperation("查询项目区位")
    @PreAuthorize("@el.check('location:list')")
    public ResponseEntity query(LocationQueryParam query, Pageable pageable){
        return new ResponseEntity<>(locationService.queryAll(query,pageable),HttpStatus.OK);
    }

    @GetMapping("/getLocation/{id}")
    public ResponseEntity getLocation(@PathVariable Long id){
        List<Location> locationList = locationService.getLocation(id);
        return new ResponseEntity<>(locationList,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目区位")
    @ApiOperation("新增项目区位")
    @PreAuthorize("@el.check('location:add')")
    public ResponseEntity create(@Validated @RequestBody LocationDto resources){
        return new ResponseEntity<>(locationService.insert(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改项目区位")
    @ApiOperation("修改项目区位")
    @PreAuthorize("@el.check('location:edit')")
    public ResponseEntity update(@Validated @RequestBody LocationDto resources){
        locationService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除项目区位")
    @ApiOperation("删除项目区位")
    @PreAuthorize("@el.check('location:del')")
    public ResponseEntity delete(@RequestBody Set<Long> ids) {
        locationService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /*
    @Log("导出项目区位")
    @ApiOperation("导出项目区位")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('location:list')")
    public void download(HttpServletResponse response, LocationQueryParam query) throws IOException {
        locationService.download(locationService.queryAll(query), response);
    }*/

}
